from django.core.management.base import BaseCommand
from scripts.timer_manager import TimerManager


class Command(BaseCommand):
    help = 'run this command to add, start, show, stop, remove, enable,disable the timed tasks ' \
           'defined in TIMERJOBSNAMELIST setting from/to systemd timers'

    def add_arguments(self, parser):
        parser.add_argument(
            'subcommand', choices=['add', 'show', 'remove', 'start', 'stop', 'enable', 'disable'])
        parser.add_argument('name', nargs='?')

    def handle(self, *args, **options):
        """
        Dispatches by given subcommand
        """
        name = options['name']
        crontab = TimerManager()
        if options['subcommand'] == 'add':
            crontab.add_timers(timer_name=name)
        elif options['subcommand'] == 'show':
            crontab.show_timers()
        elif options['subcommand'] == 'remove':
            crontab.remove_timers(timer_name=name)
        elif options['subcommand'] == 'stop':
            crontab.stop_timers(timer_name=name)
        elif options['subcommand'] == 'start':
            crontab.start_timers(timer_name=name)
        elif options['subcommand'] == 'enable':
            crontab.enable_timers(timer_name=name)
        elif options['subcommand'] == 'disable':
            crontab.disable_timers(timer_name=name)
        else:
            print(self.style.SUCCESS(self.help))
